package com.xlh.support.doccheck.controller;

import com.xlh.common.Result;
import com.xlh.exception.common.GlobalException;
import com.xlh.security.UserInfoHolder;
import com.xlh.support.doccheck.domain.SimilarFile;
import com.xlh.support.doccheck.service.DocCheckService;
import com.xlh.support.doccheck.vo.SimilarFileVO;
import com.xlh.vo.CascaderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.xlh.support.doccheck.constant.DocCheckConstant.MAX_UPLOAD_SIZE;
import static com.xlh.support.doccheck.constant.DocCheckConstant.MAX_UPLOAD_SIZE_MESSAGE;

/**
 * @author cheer
 */
@Api("文件查重")
@RestController
@RequestMapping("/doc-check")
public class DocCheckController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private DocCheckService docCheckService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result uploadFile(MultipartFile file) {
        if (file.getSize() > MAX_UPLOAD_SIZE) {
            throw new GlobalException(MAX_UPLOAD_SIZE_MESSAGE);
        }
        docCheckService.uploadFile(userInfoHolder.getUserId(), file);
        return Result.success();
    }

    @ApiOperation("查看文件")
    @GetMapping("/view")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<CascaderVO>> viewFile() {
        List<CascaderVO> fileTree = docCheckService.viewFile(userInfoHolder.getUserId());
        return Result.success(fileTree);
    }

    @ApiOperation("查询相似文件")
    @GetMapping
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<SimilarFileVO>> queryFile(@RequestParam Double similarity) {
        List<SimilarFile> similarFiles = docCheckService.querySimilarFile(userInfoHolder.getUserId(), similarity);

        List<SimilarFileVO> similarFileVOs = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(similarFiles)) {
            DecimalFormat df = new DecimalFormat("0.00%");
            for (SimilarFile similarFile : similarFiles) {
                SimilarFileVO similarFileVO = SimilarFileVO.builder()
                        .file(similarFile.getFile())
                        .comparedFile(similarFile.getComparedFile())
                        .similarity(df.format(similarFile.getSimilarityScore()))
                        .build();
                similarFileVOs.add(similarFileVO);
            }
        }

        return Result.success(similarFileVOs);
    }

    @ApiOperation("删除文件")
    @DeleteMapping
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result deleteFile() {
        docCheckService.deleteFile(userInfoHolder.getUserId());
        return Result.success();
    }
}
